package main

import (
	"fmt"
)

/**
执行用时：8 ms, 在所有 Go 提交中击败了92.86%的用户
内存消耗：4.9 MB, 在所有 Go 提交中击败了98.35%的用户
 */
func majorityElement(nums []int) []int {

	r1, r2, c1, c2 := 0, 0, 0, 0
	for _, x := range nums {
		if c1 != 0 && x == r1 {
			c1++
		} else if c2 != 0 && x == r2 {
			c2++
		} else if c1 == 0 {
			r1 = x
			c1++
		} else if c2 == 0 {
			r2 = x
			c2++
		} else {
			c1--
			c2--
		}
	}

	c1, c2 = 0, 0
	for _, x := range nums {
		if x == r1 {
			c1++
		} else if x == r2 {
			c2++
		}
	}

	res := []int{}
	if c1 > len(nums) / 3 { res = append(res, r1) }
	if c2 > len(nums) / 3 { res = append(res, r2) }

	return res
}

func main() {

	nums := []int{3, 2, 3}
	fmt.Println(majorityElement(nums))
}
